import { Controller, Get, Inject, Query } from '@nestjs/common';
import { AppService } from './app.service';
import { ClientProxy } from "@nestjs/microservices"
import { Observable } from 'rxjs';



@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Inject('USER_SERVICE')
  private userClient: ClientProxy;


   
  @Get('sum')
  calc(@Query('num') str) {
    console.log(str);
   
    this.userClient.emit('log', '求和');  

    //http.../sum?num=2,4,6,8
    const numArr = str.split(',').map((item) => parseInt(item));
    //浏览器 -> http3000 ->   ( tcp 8888 发消息给微服务并携带参数 )
    //浏览器 <- http3000 <-   ( tcp 8888 微服务计算后返回给http  )    
    return this.userClient.send('sum', numArr);
  }

}
